'''
Created on Sep 15, 2011

@author: oabalbin

it converts the hap map frequencies format to a a bed format to do the lift over
chr1:12855      0.015748031496063
'''

def create_hapmap_freq_bedfiles(file,ofile):
    '''
    1       492     rs55998931      0.0643044619422572
    chr1:13072      0.0223097112860892
    rs17854554      0.175853018372703
    '''    
    ifile = open(file)
    ofile=open(ofile,'w')
    
    for l in ifile:
        f= l.strip('\n').split('\t')
        loc,freq=f[0].split(':'), 'nors|'+f[1]
        chr,start,end=loc[0],str(int(loc[1])-1),str(int(loc[1])+1)
        ol=[chr,start,end,freq,'\n']
        ofile.write(",".join(ol).replace(',','\t'))
        
    ifile.close()
    ofile.close()

def create_hapmap_rsfreq_bedfiles(file,ofile):
    '''
    '''
    ifile = open(file)
    ofile1=open(ofile,'w')
    ofile2=open(ofile+'_rsnotfound.txt','w')
    for l in ifile:
        f= l.strip('\n').split('\t')
        if f[0]=="":
            ofile2.write(",".join(f[2:]).replace(',','\t'))
        else:
            chr,start,end,freq='chr'+f[0],str(int(f[1])-1),str(int(f[1])+1),f[2]+'|'+f[3]
            ol=[chr,start,end,freq,'\n']
            ofile1.write(",".join(ol).replace(',','\t'))
        
    
    ifile.close()
    ofile2.close()
    ofile1.close()


def reverse_bed_to_hapmap_freq():
    '''
    reverse the files on be files to 
    hapmap the format chr, pos, id.
    '''
    
    
    
    
    

'''
file1 ='/data/databases/hapmap/frequencies/EUR_freq/EUR_allele_freq_no_rsid.txt' 
file3 ='/data/databases/hapmap/frequencies/EUR_freq/EUR_allele_freq_chr1_5_pocull.txt'
file4 ='/data/databases/hapmap/frequencies/EUR_freq/EUR_allele_freq_chr6_10_pocull.txt'
file5 ='/data/databases/hapmap/frequencies/EUR_freq/EUR_allele_freq_chr11_15_pocull.txt'
file6='/data/databases/hapmap/frequencies/EUR_freq/EUR_allele_freq_chr16_20_pocull.txt'
file2 ='/data/databases/hapmap/frequencies/EUR_freq/EUR_allele_freq_rsid_chr21_22_pocull.txt'


create_hapmap_freq_bedfiles(file1,file1.replace('.txt','.bed'))
create_hapmap_rsfreq_bedfiles(file2,file2.replace('.txt','.bed'))
create_hapmap_rsfreq_bedfiles(file3,file3.replace('.txt','.bed'))
create_hapmap_rsfreq_bedfiles(file4,file4.replace('.txt','.bed'))
create_hapmap_rsfreq_bedfiles(file5,file5.replace('.txt','.bed'))
create_hapmap_rsfreq_bedfiles(file6,file6.replace('.txt','.bed'))
'''
